package com.cuijiao.emp.java.dao;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import com.cuijiao.emp.java.model.Emp;

/**
 * @author cuijiao
 *
 */
public class EmpDaoFile implements EmpDao {
    private FileData fileData = new FileData();

    @Override
    public int getIndexByEmpNo(String empNo) {
        // TODO Auto-generated method stub
        List<Emp> empList = fileData.readFile();
        if (!empList.isEmpty()) {
            return empList.indexOf(new Emp(empNo));
        } else
            return -1;

    }

    @Override
    public Emp getEmpByEmpNo(String empNo) {
        // TODO Auto-generated method stub
        List<Emp> empList = fileData.readFile();
        if (!empList.isEmpty()) {
            return empList.get(empList.indexOf(new Emp(empNo)));
        } else
            return null;
    }

    @Override
    public int deleteEmpByEmpNo(String empNo) {
        // TODO Auto-generated method stub
        List<Emp> empList = fileData.readFile();
        // 员工号存在才前往删除操作，所以数据一定非空
        empList.remove(empList.indexOf(new Emp(empNo)));
        fileData.writeFile(empList);
        return 0;
    }

    @Override
    public int modifyEmpByEmpNo(Emp emp) {
        // TODO Auto-generated method stub
        List<Emp> empList = fileData.readFile();
        // 员工号存在才前往修改操作，所以数据一定非空
        empList.set(empList.indexOf(emp), emp);
        fileData.writeFile(empList);
        return 0;
    }

    @Override
    public int addEmp(Emp emp) {
        // TODO Auto-generated method stub
        List<Emp> empList = fileData.readFile();
        empList.add(emp);
        fileData.writeFile(empList);
        return 0;
    }

    @Override
    public List<Emp> getEmps() {
        // TODO Auto-generated method stub
        return fileData.readFile();
    }

    /**
     * 内部类，文件数据，用于数据的持久化
     *
     * @author cuijiao
     *
     */
    private class FileData {
        // 写文件-保存数据
        void writeFile(List<Emp> empList) {
            File file = new File("D:\\data\\emp.data");
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            FileOutputStream fos = null;
            BufferedOutputStream bos = null;
            ObjectOutputStream oos = null;
            try {
                fos = new FileOutputStream(file);
                bos = new BufferedOutputStream(fos);
                oos = new ObjectOutputStream(bos);
                oos.writeObject(empList);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    if (oos != null) {
                        oos.close();
                        oos = null;
                    }
                    if (bos != null) {
                        bos.close();
                        bos = null;
                    }
                    if (fos != null) {
                        fos.close();
                        fos = null;
                    }

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        // 读文件-提取数据
        List<Emp> readFile() {
            File file = new File("D:\\data\\emp.data");
            List<Emp> emplist = new ArrayList<>();// 数据对象
            if (!file.exists()) {// 文件不存在，无数据
                return emplist;
            }
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            ObjectInputStream ios = null;

            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);// 过滤流，加速文件写操作
                ios = new ObjectInputStream(bis);// 对象流
                emplist = (List<Emp>) ios.readObject();

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                emplist = new ArrayList<>();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                emplist = new ArrayList<>();
            } finally {
                try {// 关闭流，释放资源
                    if (ios != null) {
                        ios.close();
                        ios = null;
                    }
                    if (bis != null) {
                        bis.close();
                        bis = null;
                    }
                    if (fis != null) {
                        fis.close();
                        fis = null;
                    }

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return emplist;
            }

        }

    }
}
